package com.example.task1.ui.AllService.traffic;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.TrafficUndoListBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.TimeZone;

import retrofit2.Call;

public class TrafficUndoListAdapter extends BaseAdapter<TrafficUndoListBean.RowsDTO> {

    private Context context;
    private List<TrafficUndoListBean.RowsDTO> mData = new ArrayList<>();
    private TextView tvTrafficOffence;
    private TextView tvStatus;
    private TextView tvLicencePlate;
    private TextView tvIllegalSites;
    private TextView tvCatType;
    private TextView tvPerformDate;
    private Button btnUndo;
    private Button btnAcceptance;

    public TrafficUndoListAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void setData(List<TrafficUndoListBean.RowsDTO> rows) {
        int size = mData.size();
        mData.addAll(rows);
        notifyItemInserted(size);
    }

    @Override
    protected int getLayout() {
        return R.layout.trafficundolist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<TrafficUndoListBean.RowsDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, TrafficUndoListBean.RowsDTO date) {

    }

    @Override
    protected void onBind(myHolder holder, TrafficUndoListBean.RowsDTO date, int position) {
        tvTrafficOffence.setText(date.getTrafficOffence());
        tvStatus.setText(date.getDisposeState());
        tvLicencePlate.setText("车牌：" + date.getLicencePlate());
        tvCatType.setText("号牌种类：" + date.getCatType());
        tvIllegalSites.setText("违章地点" + date.getIllegalSites());
        tvPerformDate.setText("处罚日期：" + date.getPerformDate());
        if ("已缴款".equals(date.getDisposeState())) {
            btnUndo.setVisibility(View.GONE);
            btnAcceptance.setVisibility(View.GONE);
        }

        btnAcceptance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/traffic/illegal/accept/" + date.getId(),Api.getBody(null));
                Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        MyApplication.show("接受成功");
                        notifyItemChanged(position);
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });

        btnUndo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                EditText editText = new EditText(context);
                editText.setHint("输入撤销原因");
                builder.setView(editText);
                builder.setNegativeButton("提交", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String s = editText.getText().toString();
                        if (TextUtils.isEmpty(s)) {
                            MyApplication.show("撤销原因不能为空");
                        }else{
                            HashMap<String, Object> map = new HashMap<>();
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
                            map.put("applyDate",simpleDateFormat.format(new Date()));
                            map.put("illegalId",date.getId());
                            map.put("reason",s);
                            Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/traffic/illegal/cancel",Api.getBody(map));
                            Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                                @Override
                                public void success(ApiResp data) {
                                    MyApplication.show("提交成功");
                                    notifyItemChanged(position);
                                }

                                @Override
                                public void error(String msg) {

                                }
                            });

                        }
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    protected void holder(View itemView) {
        initView(itemView);
    }

    private void initView(View view) {
        tvTrafficOffence = (TextView) view.findViewById(R.id.tv_trafficOffence);
        tvStatus = (TextView) view.findViewById(R.id.tv_status);
        tvLicencePlate = (TextView) view.findViewById(R.id.tv_licencePlate);
        tvIllegalSites = (TextView) view.findViewById(R.id.tv_illegalSites);
        tvCatType = (TextView) view.findViewById(R.id.tv_catType);
        tvPerformDate = (TextView) view.findViewById(R.id.tv_performDate);
        btnUndo = (Button) view.findViewById(R.id.btn_undo);
        btnAcceptance = (Button) view.findViewById(R.id.btn_acceptance);
    }
}
